"------------------------------------------------------------------------------
" This is a syntax file for vim with additions for both markdown and text files.
"
" So that it can function properly, and so we can take advantage of the already
" present configurations for both markdown and text files, place in the
" following paths:
"
" For text files:
" ${HOME}/.vim/after/ftplugin/text.vim
"
" For markdown files:
" ${HOME}/.vim/after/ftplugin/markdown.vim
"
" This file works for both plain text and markdown (since I use the same
" additions in both). Feel free to create a symlink to either of them. For
" example:
"
" cd ${HOME}/.vim/after/ftplugin/text.vim
" ln -s text.vim markdown.vim 
"------------------------------------------------------------------------------

"------------------------------------------------------------------------------
" Configuration
"------------------------------------------------------------------------------

" Folding
set foldmethod=indent

" Uses spaces instead of tabs
set expandtab tabstop=2 shiftwidth=2

"------------------------------------------------------------------------------
" Formatting
"------------------------------------------------------------------------------

" No autoclosing on '
inoremap ' '

" Formating options
set fo+=t " Automatic formatting of text
set fo+=n " Recognize numbered lists

" Toggle automatic formatting
"-----------------------------------------------------------------------------

set fo-=a
nnoremap <leader>f :call ToggleAutoFormat()<cr>

fu! ToggleAutoFormat()
	if stridx(&fo, 'a') == -1
		set fo+=a
	else
		set fo-=a
	endif
endf

" Lists
"-----------------------------------------------------------------------------

" Comment formatting needs to be disabled, otherwise it interferes with
" formatlistpat
set comments=

" We use /usr/share/vim/vim91/ftplugin/markdown.vim as a starting point
"-----------------------------------------------------------------------------


" Handle todo lists in the style of: - [ ] Todo text
let &formatlistpat='^\s*-\s\[[X ]\]\s\+'

" This is nearly the default, only with the bullet point icon (•) added
let &formatlistpat..='\|^\s*[-*+•]\s\+'

" Exactly like the default, matches number lists: 1. some text\n2. More text
let &formatlistpat..='\|^\s*\d\+\.\s\+'

" Although I don't exactly what this regex matches, I put it as is.
let &formatlistpat..='\|^\[^\ze[^\]]\+\]:\&^.\{4\}'


"------------------------------------------------------------------------------
" Mappings
"------------------------------------------------------------------------------

" Center text
nnoremap <C-c> :center<cr>

"Special characters
inoremap \Ep Σ
inoremap \ep ε
inoremap \Ee ∈ 
inoremap \ee ϵ
inoremap \delta ∆

inoremap \fore ⸫
inoremap \-> →
inoremap \/> ↙
inoremap \\> ↘
inoremap \|> ↓
inoremap \=> ⇒

inoremap \. •<Space>
inoremap \par ¶
inoremap \sec §

inoremap \_1 ₁
inoremap \_2 ₂

inoremap \^1 ¹

"------------------------------------------------------------------------------
" Syntax for text files
"------------------------------------------------------------------------------

syn match myTodo /^.*TODO:.*/
hi myTodo cterm=bold ctermbg=red ctermfg=white

" Horizontal lines
inoremap \hl <esc>:call FillLine('─')<cr>i
inoremap \dl <esc>:call FillLine('-')<cr>i

" Fills text width with passed char
fu! FillLine(char)
	for i in range(0, &textwidth - 1)
		execute 'norm!i' .. a:char
	endfor
endf
